package com.js.data.system.service.impl;

import com.js.data.common.exception.ApiException;
import com.js.data.system.dto.AreaDTO;
import com.js.data.system.entity.SysArea;
import com.js.data.system.mapper.SysAreaMapper;
import com.js.data.system.service.AreaService;
import com.js.data.system.vo.AreaTreeVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * AreaServiceImpl-地区表 服务实现类  --本类复制1.0版本地区业务实现
 *
 * @author wujj
 * @version 1.0.0
 * @since 2020/6/3 19:09
 */
@Service
public class AreaServiceImpl extends ServiceImpl<SysAreaMapper, SysArea> implements AreaService {


    /**
     * 新增地区
     *
     * @param areaDTO 地区表单
     */
    @Override
    public void saveArea(AreaDTO areaDTO) {
        SysArea sysArea = new SysArea();
        BeanUtils.copyProperties(areaDTO, sysArea);
        Long parentId = areaDTO.getParentId();
        SysArea parentArea = null;
        if (parentId != null && parentId != 0) {
            parentArea = getById(parentId);
            Optional.ofNullable(parentArea).orElseThrow(() -> new ApiException("父地区不存在"));
            sysArea.setParentIds(parentArea.getParentIds() + "," + parentId);
        }
        save(sysArea);
        // 数据保存成功,父级"直接子集数量"加1
        if (Objects.nonNull(parentArea)) {
            getBaseMapper().incrSubCount(parentId);
        }
    }

    /**
     * 地区树形
     *
     * @param parentId 父id
     * @return List<AreaTreeVO>
     */
    @Override
    public List<AreaTreeVO> treeArea(Long parentId) {
        if (Objects.isNull(parentId)) {
            parentId = 0L;
        }
        LambdaQueryWrapper<SysArea> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysArea::getParentId, parentId).orderByAsc(SysArea::getSortBy);
        List<AreaTreeVO> retList = new ArrayList<>();
        list(queryWrapper).forEach(dept -> {
            AreaTreeVO areaTreeVO = new AreaTreeVO();
            BeanUtils.copyProperties(dept, areaTreeVO);
            retList.add(areaTreeVO);
        });
        return retList;
    }

    /**
     * 修改地区
     *
     * @param id      地区id
     * @param areaDTO 地区表单
     */
    @Override
    public void editArea(Long id, AreaDTO areaDTO) {
        Long newParentId = areaDTO.getParentId();
        if (id.equals(newParentId)) {
            throw new ApiException("上级不能为自己");
        }
        SysArea sysArea = new SysArea();
        BeanUtils.copyProperties(areaDTO, sysArea);
        sysArea.setId(id);
        updateById(sysArea);
    }

    /**
     * 删除地区
     *
     * @param id 地区id
     */
    @Override
    public void deleteArea(Long id) {
        // 删除子节点
        SysArea sysArea = getById(id);
        Optional.ofNullable(sysArea).orElseThrow(() -> new ApiException("地区不存在"));
        removeById(id);
        List<Long> childIdList = getBaseMapper().selectAllChildIdByPid(id);
        this.removeByIds(childIdList);
        if (0 != sysArea.getParentId()) {
            SysArea parentDept = getById(sysArea.getParentId());
            Optional.ofNullable(parentDept).orElseThrow(() -> new ApiException("父地区不存在"));
            getBaseMapper().decrSubCount(parentDept.getParentId());
        }
        childIdList.add(id);
    }
}
